home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / LSSEARCH.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  143 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lssearch.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* non-ansi headers */
  18. #include <bool.h>
  19.  
  20. /* library headers */
  21. #include <blkio.h>
  22.  
  23. /* local headers */
  24. #include "lseq_.h"
  25.  
  26. /*man---------------------------------------------------------------------------
  27. NAME
  28.      lssearch - lseq search
  29.  
  30. SYNOPSIS
  31.      #include <lseq.h>
  32.  
  33.      int lssearch(lsp, offset, buf, bufsize, cmp)
  34.      lseq_t *lsp;
  35.      size_t offset;
  36.      const void *buf;
  37.      size_t bufsize;
  38.      int (*cmp)(const void *p1, const void *p2, size_t n);
  39.  
  40. DESCRIPTION
  41.      The lssearch function performs a linear search through lseq lsp
  42.      for a record with a field matching the field pointed to by buf.
  43.      The field being searched begins offset characters from the
  44.      beginning of the record and is bufsize characters long.  The
  45.      function pointed to by cmp is used to test for a match.  The
  46.      search begins on the record following the current record, so
  47.      before the first call to lssearch the cursor should be set to
  48.      null, and successive calls will find succesive records with
  49.      fields matching buf.  If the field is matched, the cursor is left
  50.      on the record with the matching field.
  51.  
  52.      The user supplied comparison function cmp must be of the
  53.      following form:
  54.  
  55.           int cmp(const void *p1, const void *p2, size_t n);
  56.  
  57.      where p1 and p2 point to the two keys to bey compared and n is
  58.      the key size.  The return must be less than, equal to, or greater
  59.      than zero if p1 is less than, equal to, or greater than p2,
  60.      respectively.  If cmp is NULL then the memcmp function is used as
  61.      the default.
  62.  
  63.      lssearch will fail if one or more of the following is true:
  64.  
  65.      [EINVAL]       lsp is not a valid lseq pointer.
  66.      [EINVAL]       buf is the NULL pointer.
  67.      [EINVAL]       bufsize is less than 1.
  68.      [LSEBOUND]     offset + bufsize extends beyond the
  69.                     end of the record.
  70.      [LSELOCK]      lsp is not read locked.
  71.      [LSENOPEN]     lsp is not open.
  72.  
  73. SEE ALSO
  74.      lscursor.
  75.  
  76. DIAGNOSTICS
  77.      Upon successful completion, a value of 1 is returned if the field
  78.      was matched or a value of 0 if it was not.  Otherwise, a value of
  79.      -1 is returned, and errno set to indicate the error.
  80.  
  81. ------------------------------------------------------------------------------*/
  82. #ifdef AC_PROTO
  83. int lssearch(lseq_t *lsp, size_t offset, const void *buf, size_t bufsize, lscmp_t cmp)
  84. #else
  85. int lssearch(lsp, offset, buf, bufsize, cmp)
  86. lseq_t *lsp;
  87. size_t offset;
  88. const void *buf;
  89. size_t bufsize;
  90. lscmp_t cmp;
  91. #endif
  92. {
  93.     bool found = FALSE;
  94.  
  95.     /* validate arguments */
  96.     if (!ls_valid(lsp) || buf == NULL || bufsize < 1) {
  97.         errno = EINVAL;
  98.         return -1;
  99.     }
  100.  
  101.     /* check if not open */
  102.     if (!(lsp->flags & LSOPEN)) {
  103.         errno = LSENOPEN;
  104.         return -1;
  105.     }
  106.  
  107.     /* check if not read locked */
  108.     if (!(lsp->flags & LSRDLCK)) {
  109.         errno = LSELOCK;
  110.         return -1;
  111.     }
  112.  
  113.     /* check if over record boundary */
  114.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  115.         errno = LSEBOUND;
  116.         return -1;
  117.     }
  118.  
  119.     /* check if cmp is NULL */
  120.     if (cmp == NULL) {
  121.         cmp = memcmp;
  122.     }
  123.  
  124.     /* advance cursor one record */
  125.     if (lsnext(lsp) == -1) {
  126.         LSEPRINT;
  127.         return -1;
  128.     }
  129.  
  130.     while (lsp->clspos != NIL) {
  131.         if ((*cmp)(((char *)lsp->clsrp->recbuf + offset), buf, bufsize) == 0) {
  132.             found = TRUE;
  133.             break;
  134.         }
  135.         if (lsnext(lsp) == -1) {
  136.             LSEPRINT;
  137.             return -1;
  138.         }
  139.     }
  140.  
  141.     return found ? 1 : 0;
  142. }
  143.